package Topics;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("162.14.99.141");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("ty");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("topics.exchange", BuiltinExchangeType.TOPIC, true, false, false, null);

        channel.queueDeclare("topics.queue1", true, false, false, null);
        channel.queueDeclare("topics.queue2", true, false, false, null);
        channel.queueDeclare("topics.queue3", true, false, false, null);

        //交换机和队列进行绑定, 这里的routing key 可以为任意字符串
        channel.queueBind("topics.queue1", "topics.exchange", "order.*");
        channel.queueBind("topics.queue2", "topics.exchange", "order.*.*");
        channel.queueBind("topics.queue3", "topics.exchange", "order.#");

        channel.basicPublish("topics.exchange", "order", null, "msg: order".getBytes());
        channel.basicPublish("topics.exchange", "order.fail", null, "msg: order.fail".getBytes());
        channel.basicPublish("topics.exchange", "order.fail", null, "msg: order.success".getBytes());
        channel.basicPublish("topics.exchange", "order.success.result", null, "msg: order.success.result".getBytes());

        channel.close();
        connection.close();
    }
}
